/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.stripes.action;

import able.stripes.JSP;
import com.google.inject.Inject;
import exatrez.dao.DepartmentDAO;
import exatrez.dao.PenggunaDAO;
import exatrez.dao.RefShipVisitSubDAO;
import exatrez.dao.ShipVisit3CounterDAO;
import exatrez.dao.ShipVisit3DAO;
import exatrez.dao.ShipVisit3FileDAO;
import exatrez.dao.ShipVisit3FileTempDAO;
import exatrez.dao.ShipVisit3SubDAO;
import exatrez.dao.ShipVisit3SubExtendDAO;
import exatrez.dao.ShipVisit3SubExtendFileDAO;
import exatrez.dao.ShipVisit3SubExtendFileTempDAO;
import exatrez.manager.ShipVisit3Manager;
import exatrez.model.InfoAudit;
import exatrez.model.RefShipVisitMatch;
import exatrez.model.RefShipVisitMatchSub;
import exatrez.model.RefShipVisitSub;
import exatrez.model.ShipVisit3;
import exatrez.model.ShipVisit3Counter;
import exatrez.model.ShipVisit3File;
import exatrez.model.ShipVisit3FileTemp;
import exatrez.model.ShipVisit3History;
import exatrez.model.ShipVisit3Sub;
import exatrez.model.ShipVisit3SubExtend;
import exatrez.model.ShipVisit3SubExtendFile;
import exatrez.model.ShipVisit3SubExtendFileTemp;
import exatrez.model.User;
import exatrez.stripes.BaseActionBean;
import exatrez.stripes.Configuration;
import exatrez.stripes.ShipVisitConfiguration;
import exatrez.stripes.config.AuthRequired;
import exatrez.stripes.util.EmailUtil;
import exatrez.stripes.util.FileUtil;
import freemarker.cache.FileTemplateLoader;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import java.io.File;
import java.io.StringWriter;
import java.io.Writer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.FileBean;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Mansur
 */
@UrlBinding("/shipVisit3Flow")
@AuthRequired
public class ShipVisit3FlowAction extends BaseActionBean{
    
    private static Logger LOG = LoggerFactory.getLogger(ShipVisit3FlowAction.class);
    
    @Inject
    ShipVisitConfiguration svconf;
    @Inject
    Configuration conf;
    @Inject
    private ShipVisit3Manager sv3Manager;
    @Inject
    private DepartmentDAO departmentDAO;
    @Inject
    private ShipVisit3CounterDAO shipVisit3CounterDAO;
    @Inject
    private ShipVisit3DAO shipVisit3DAO;
    @Inject
    private ShipVisit3SubDAO shipVisit3SubDAO;
    @Inject
    private ShipVisit3SubExtendDAO shipVisit3SubExtendDAO;
    @Inject
    private EmailUtil emailUtil;
    @Inject
    private PenggunaDAO penggunaDAO;
    @Inject
    private RefShipVisitSubDAO refShipVisitSubDAO;
    @Inject
    private ShipVisit3FileTempDAO shipVisit3FileTempDAO;
    @Inject
    private ShipVisit3SubExtendFileTempDAO subFileTempDAO;
    @Inject
    private ShipVisit3FileDAO shipVisit3FileDAO;
    @Inject
    private ShipVisit3SubExtendFileDAO shipVisit3SubExtendFileDAO;
    
    private static final String NEWVISIT = "visitFlow3/new_visit.jsp";
    private static final String LISTKIV = "visitFlow3/list_kiv.jsp";
    private static final String LISTAPPROVE = "visitFlow3/list_approve.jsp";
    private static final String LISTREJECT = "visitFlow3/list_reject.jsp";
    private static final String EDITVISIT = "visitFlow3/edit_visit.jsp";
    private static final String LISTSTAFFKIV = "visitFlow3/list_staff_kiv.jsp";
    private static final String DETAILSTAFF = "visitFlow3/detail_staff_visit.jsp";
    private static final String SEARCHSTAFF = "visitFlow3/search_staff.jsp";
    private static final String SEARCHVISIT = "visitFlow3/search_visit.jsp";
    private static final String NEWDOC = "visitFlow3/new_doc.jsp";
    private static final String NEWDOCSUB = "visitFlow3/new_doc_sub.jsp";
    private static final String VIEWEDITDOC = "visitFlow3/view_edit_doc.jsp";
    private static final String VIEWEDITDOCSUB = "visitFlow3/view_edit_doc_sub.jsp";
    
    private ShipVisit3 shipVisit3;
    private ShipVisit3Sub shipVisit3Sub;
    private ShipVisit3SubExtend shipVisit3SubExtend;
    private ShipVisit3Counter shipVisit3Counter;
    private User staffSelected;
    
    private String shipVisit3Id;
    private FileBean document;
    private String currentYear = String.valueOf(new Date().getYear()+1900);
    private String staffId;
    private Integer defaultCount = 0;
    private Integer counterId;
    private String departmentId;
    private Integer sizeMatch;
    private Integer sizeSubMatch;
    private String editFlag;    // 0 = viewOnly, 1 = edit
    private String fromStage;
    private String statusDesc;
    private String statusApproval;
    private String comment;
    private String refSubId;
    private String subExtendId;
    
    private List<RefShipVisitMatch> listShipVisitMatch;
    private List<ShipVisit3> listShipVisit3;
    private List<ShipVisit3Sub> listSub;
    private List<ShipVisit3SubExtend> listSubExtend;
    private List<String> listDetails;
    private List<String> listSubDetails;
    private List<ShipVisit3Counter> listCounter;
    private List<ShipVisit3> listKIV;
    private List<ShipVisit3> listApprove;
    private List<ShipVisit3> listReject;
    private List<ShipVisit3> listStaffVisited;
    private List<ShipVisit3FileTemp> listVisitFiletmp;
    private List<ShipVisit3SubExtendFileTemp> listVisitFileSubtmp;
    
    private static final String FOLDERNAME = "shipVisit3";
    private static final String FOLDERNAMESUB = "subItems";
    //    standard contenttype
    private String forXlsx = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    private String forDocx = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
    private String forPptx = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
    private String forXls = "application/vnd.ms-excel";
    private String forDoc = "application/msword";
    private String forPpt = "application/vnd.ms-powerpoint";
    private String forPdf = "application/pdf";
    private String forVideoMov = "video/quicktime";
    private String forVideoMkv = "video/x-matroska";
    private String forVideoMp4 = "video/mp4";
    private String forVideoAvi = "video/x-msvideo";
    private String forVideoMpeg= "video/mpeg";
    private String forVideoWmv = "application/octet-stream";
    private String forVideoAsf = "video/x-ms-asf";
    private String forVideoFlv = "video/x-flv";
    private String forVideoRmvb= "application/vnd.rn-realmedia-vbr";
    private String forVideo3gp = "video/3gpp";
    
    @DefaultHandler
    public Resolution newVisit(){
        LOG.debug("method newVisit start");
        User user = getContext().getCurrentUser();
        staffId = user.getUserId();
        if(user.getDepartment().getShipVisitProcessFlow().equals("T")){ //ship visit using process flow
            listCounter = sv3Manager.getListCounter(user, currentYear, departmentId);
            this.checkListCounter(listCounter,user);
            for (ShipVisit3Counter sv3c : listCounter) {
                counterId = sv3c.getSv3CounterId();
            }
        }
        listShipVisitMatch = sv3Manager.getListShipVisitMatch(user.getDepartment().getDepartmentId());
        sizeMatch = listShipVisitMatch.isEmpty() ? 999999 : listShipVisitMatch.size();
        sizeSubMatch = 999999;
        for (RefShipVisitMatch rsvm : listShipVisitMatch) {
            if(rsvm.getListMatchSub().size() > 0)
                sizeSubMatch = rsvm.getListMatchSub().size();
        }
        LOG.debug("method newVisit finish");
        return new JSP(NEWVISIT);
    }
    
    private void checkListCounter(List<ShipVisit3Counter> list, User selectedUser){        
        String result = null;
        User user = getContext().getCurrentUser();
        if(list.size() == 0){
            ShipVisit3Counter visitCounter = new ShipVisit3Counter();
            visitCounter.setUser(selectedUser);
            visitCounter.setVisited(defaultCount);
            visitCounter.setRejected(defaultCount);
            visitCounter.setYear(currentYear);
            visitCounter.setTarget(departmentDAO.findById(selectedUser.getDepartment().getDepartmentId()).getTarget());
            
            InfoAudit ia = new InfoAudit();
            ia.setCreateId(user);
            ia.setCreateDate(new Date());
            visitCounter.setInfoAudit(ia);
            result = sv3Manager.saveOrUpdateVisitCounter(visitCounter);
            LOG.debug("(checkListCounter)result new visitCounter :"+result);
            listCounter = sv3Manager.getListCounter(selectedUser, currentYear, departmentId);
        }else{
            if(list.get(0).getTarget().compareTo(selectedUser.getDepartment().getTarget()) == 0){
                result = selectedUser.getUserId()+", bingo.. the value is pair";
                LOG.debug("(checkListCounter)result visitCounter :"+result);
            }else{
                shipVisit3Counter = shipVisit3CounterDAO.findById(listCounter.get(0).getSv3CounterId());
                shipVisit3Counter.setTarget(selectedUser.getDepartment().getTarget());
                 InfoAudit iaU = shipVisit3Counter.getInfoAudit();
                 iaU.setUpdateDate(new Date());
                 iaU.setUpdateId(user);
                result = sv3Manager.saveOrUpdateVisitCounter(shipVisit3Counter);
                LOG.debug("(checkListCounter)result update visitCounter :"+result);
                listCounter = sv3Manager.getListCounter(selectedUser, currentYear, departmentId);
            }
        }
    }
    
    public Resolution submit(){
        User user = getContext().getCurrentUser();
        LOG.debug("method submit start > user.id :"+user.getUserId());
        
        InfoAudit ia = new InfoAudit();
        ia.setCreateDate(new Date());
        ia.setCreateId(user);
        
        if(user.getDepartment().getShipVisitProcessFlow().equals("T")){ //ship visit using process flow
            listCounter = sv3Manager.getListCounter(user, currentYear, departmentId);
            this.checkListCounter(listCounter,user);
            for (ShipVisit3Counter c : listCounter) {
                counterId = c.getSv3CounterId();
            }
        }
        
        try{
            ShipVisit3 sv3 = new ShipVisit3();
            sv3.setDeleted("N");
            sv3.setStatus("K");
            sv3.setUser(user);
            sv3.setDepartmentCurrent(user.getDepartment());
            sv3.setWorkflow(user.getDepartment().getShipVisitProcessFlow());
            sv3.setDateVisit(shipVisit3.getDateVisit());
            sv3.setVessel(shipVisit3.getVessel());
            sv3.setLocation(shipVisit3.getLocation());
            listShipVisitMatch = sv3Manager.getListShipVisitMatch(user.getDepartment().getDepartmentId());
            sizeMatch = listShipVisitMatch.isEmpty() ? 999999 : listShipVisitMatch.size();
            sizeSubMatch = 999999;
            for (RefShipVisitMatch rsvm : listShipVisitMatch) {
                if(rsvm.getListMatchSub().size() > 0)
                    sizeSubMatch = rsvm.getListMatchSub().size();
            }
            List<ShipVisit3Sub> sub = new ArrayList<ShipVisit3Sub>();
            int i = 0;
            for (RefShipVisitMatch rsvm : listShipVisitMatch) {
                ShipVisit3Sub sv3s = new ShipVisit3Sub();
                List<ShipVisit3SubExtend> sv3se = new ArrayList<ShipVisit3SubExtend>();
                int j = 0;
                for (RefShipVisitMatchSub ms : rsvm.getListMatchSub()) {
                    ShipVisit3SubExtend se = new ShipVisit3SubExtend();
                    se.setShipVisitSub(ms.getShipVisitSub());
                    se.setInputType(ms.getShipVisitSub().getInputType());
                    LOG.debug("listSubDetails.get("+j+") = "+this.listSubDetails.get(j));
                    se.setDetails(this.listSubDetails.get(j));
                    se.setShipVisit3Sub(sv3s);
                    se.setInfoAudit(ia);
                    sv3se.add(se);
                    j++;
                }
                sv3s.setListSV3SubExtend(sv3se);
                sv3s.setRefShipVisitHeader(rsvm.getShipVisitHeader());
                LOG.debug("listDetails.get("+i+") = "+this.listDetails.get(i));
                sv3s.setDetails(listDetails.get(i));
                sv3s.setInputType(rsvm.getShipVisitHeader().getInputType());
                sv3s.setInfoAudit(ia);
                sub.add(sv3s);
                i++;
            }
            sv3.setInfoAudit(ia);
            
            listVisitFiletmp = sv3Manager.getListFiletmp(user.getUserId());
            listVisitFileSubtmp = sv3Manager.getListFileSubtmp(user.getUserId(), null);
            LOG.debug("sub.size :"+sub.size());
            LOG.debug("listVisitFiletmp.size :"+listVisitFiletmp.size());
            LOG.debug("listVisitFileSubtmp.size :"+listVisitFileSubtmp.size());
            
            String result = sv3Manager.saveForm(sv3, sub, listVisitFiletmp, listVisitFileSubtmp);
            if(result.equals("success")){
                if(user.getDepartment().getShipVisitProcessFlow().equals("T")) //ship visit using process flow
                    addSimpleMessage(svconf.getProperty("form.success"));
                else
                    addSimpleMessage(svconf.getProperty("success"));
            }else{
                addSimpleError(svconf.getProperty("form.error"));
                LOG.debug("method submit finish > user.id :"+user.getUserId());
                return new JSP(NEWVISIT);
            }
        }catch(NullPointerException npx){
            LOG.error(npx.getMessage());
            npx.printStackTrace(); // for development used only
            addSimpleError(svconf.getProperty("form.error.null"));
            LOG.debug("method submit finish > user.id :"+user.getUserId());
            return new JSP(NEWVISIT);
        }catch(Exception ex){
            LOG.error(ex.getMessage());
            ex.printStackTrace();//for development use only
            addSimpleError(svconf.getProperty("form.error"));
            LOG.debug("method submit finish > user.id :"+user.getUserId());
            return new JSP(NEWVISIT);
        }
        LOG.debug("method submit finish > user.id :"+user.getUserId());
        return new RedirectResolution(ShipVisit3FlowAction.class, "visitKiv");
    }
    
    public Resolution visitKiv(){
        User user = getContext().getCurrentUser();
        LOG.debug("user :"+user.getUserId());
        LOG.debug("currentYear :"+currentYear);
        listKIV = sv3Manager.getListVisitKIV(user, currentYear);
        LOG.debug("listKIV :"+listKIV.size());
        if(user.getDepartment().getShipVisitProcessFlow().equals("T")){
            listCounter = sv3Manager.getListCounter(user, currentYear, departmentId);
            this.checkListCounter(listCounter,user);
            for (ShipVisit3Counter c : listCounter) {
                counterId = c.getSv3CounterId();
            }
        }
        return new JSP(LISTKIV);
    }
    
    public Resolution visitApprove(){
        User user = getContext().getCurrentUser();
        listApprove = sv3Manager.getListVisitApprove(user, currentYear);
        listCounter = sv3Manager.getListCounter(user, currentYear, departmentId);
        this.checkListCounter(listCounter, user);
        for (ShipVisit3Counter c : listCounter) {
            counterId = c.getSv3CounterId();
        }
        return new JSP(LISTAPPROVE);
    }
    
    public Resolution visitReject(){
        User user = getContext().getCurrentUser();
        listReject = sv3Manager.getListVisitReject(user, staffId);
        listCounter = sv3Manager.getListCounter(user, currentYear, departmentId);
        this.checkListCounter(listCounter,user);
        for (ShipVisit3Counter c : listCounter) {
            counterId = c.getSv3CounterId();
        }
        return new JSP(LISTREJECT);
    }
    
    public Resolution viewEditForm(){        
        User user = getContext().getCurrentUser();
        LOG.debug("method viewEditForm start > user.id :"+user.getUserId());
        staffId = user.getUserId();
        
        if(user.getDepartment().getShipVisitProcessFlow().equals("T")){
            listCounter = sv3Manager.getListCounter(user, currentYear, departmentId);
            this.checkListCounter(listCounter,user);
            for (ShipVisit3Counter sv3c : listCounter) {
                counterId = sv3c.getSv3CounterId();
            }
        }        
        
        shipVisit3Id = getContext().getRequest().getParameter("idVisit");
        editFlag = getContext().getRequest().getParameter("flagEdit");
        fromStage = getContext().getRequest().getParameter("stage");
        try{
            int sv3Id = Integer.parseInt(shipVisit3Id);
            shipVisit3 = shipVisit3DAO.findById(sv3Id);
            listSub = shipVisit3.getListShipVisitSub();
            for (ShipVisit3Sub sv3s : listSub) {
                if(sv3s.getListSV3SubExtend().size() > 0){
                    listSubExtend = sv3s.getListSV3SubExtend();
                }
            }
        }catch(Exception ex){
            LOG.error(ex.getMessage());
            ex.printStackTrace();//for development use only
            addSimpleError(svconf.getProperty("form.error"));
            return new JSP(LISTKIV);
        }
        
        LOG.debug("method viewEditForm finish > user.id :"+user.getUserId());
        return new JSP(EDITVISIT);
    }
    
    public Resolution saveEditForm(){
        User user = getContext().getCurrentUser();
        LOG.debug("method saveEditForm start > user.id :"+user.getUserId());
        
        LOG.debug("shipVisit3 :"+shipVisit3Id);
        try{
            ShipVisit3 sv = shipVisit3DAO.findById(Integer.parseInt(shipVisit3Id));
            InfoAudit ia = sv.getInfoAudit();
            ia.setUpdateId(user);
            ia.setUpdateDate(new Date());
            
            sv.setInfoAudit(ia);
            
            int i = 0;
            for (ShipVisit3Sub sv3s : sv.getListShipVisitSub()) {
                InfoAudit ias = sv3s.getInfoAudit();
                ias.setUpdateId(user);
                ias.setUpdateDate(new Date());
                if(!sv3s.getDetails().equals(listDetails.get(i))){
                    LOG.debug(sv3s.getRefShipVisitHeader().getDescription()+" => Ori :"+sv3s.getDetails()+" , Change :"+listDetails.get(i));
                    sv3s.setDetails(listDetails.get(i));
                    sv3s.setInfoAudit(ias);
                }
                int j = 0;
                for (ShipVisit3SubExtend sv3se : sv3s.getListSV3SubExtend()) {
                    InfoAudit iase = sv3se.getInfoAudit();
                    iase.setUpdateId(user);
                    iase.setUpdateDate(new Date());
                    if(!sv3se.getDetails().equals(listSubDetails.get(j))){
                        LOG.debug(sv3se.getShipVisit3SubExtId()+" => Ori :"+sv3se.getDetails()+" , Change :"+listSubDetails.get(j));
                        sv3se.setDetails(listSubDetails.get(j));
                        sv3se.setInfoAudit(iase);
                    }
                    j++;
                }
                i++;
//                for (ShipVisit3Sub sv3s_c : shipVisit3.getListShipVisitSub()) {
//                    if(sv3s.getShipVisit3SubId().equals(sv3s_c.getShipVisit3SubId()) && !sv3s.getDetails().equals(sv3s_c.getDetails())){
//                        LOG.debug(sv3s.getRefShipVisitHeader().getDescription()+" => Ori :"+sv3s.getDetails()+" , Change :"+sv3s_c.getDetails());
//                        sv3s.setDetails(sv3s_c.getDetails());
//                        sv3s.setInfoAudit(ias);
//                    }
//                    for (ShipVisit3SubExtend sv3se : sv3s.getListSV3SubExtend()) {
//                        InfoAudit iase = sv3se.getInfoAudit();
//                        iase.setUpdateId(user);
//                        iase.setUpdateDate(new Date());
//                        for (ShipVisit3SubExtend sv3se_c : sv3s_c.getListSV3SubExtend()) {
//                            if(sv3se.getShipVisit3SubExtId().equals(sv3se_c.getShipVisit3SubExtId()) && !sv3se.getDetails().equals(sv3se_c.getDetails())){
//                                LOG.debug(sv3se.getShipVisit3SubExtId()+" => Ori :"+sv3se.getDetails()+" , Change :"+sv3se_c.getDetails());
//                                sv3se.setDetails(sv3se_c.getDetails());
//                                sv3se.setInfoAudit(iase);
//                            }
//                        }
//                    }
//                }
            }
            
//            for (ShipVisit3Sub sv3s : shipVisit3.getListShipVisitSub()) {
//                InfoAudit ias = sv3s.getInfoAudit();
//                ias.setUpdateId(user);
//                ias.setUpdateDate(new Date());
//                for (ShipVisit3Sub sv3s_ori : sv3.getListShipVisitSub()) {
//                    if(!sv3s.getDetails().equals(sv3s_ori.getDetails())){
//                        sv3s.setInfoAudit(ias);
//                    }
//                    
//                    for (ShipVisit3SubExtend sv3se : sv3s.getListSV3SubExtend()) {
//                        InfoAudit iase = sv3se.getInfoAudit();
//                        iase.setUpdateId(user);
//                        iase.setUpdateDate(new Date());
//                        for (ShipVisit3SubExtend sv3se_ori : sv3s_ori.getListSV3SubExtend()) {
//                            if(!sv3se.getDetails().equals(sv3se_ori.getDetails())){
//                                sv3se.setInfoAudit(iase);
//                            }
//                        }
//                    }
//                }
//            }
            
//            String result = sv3Manager.updateForm(sv);
//            if(result.equals("success")){
//                addSimpleMessage(svconf.getProperty("form.success"));
//            }else{
//                addSimpleError(svconf.getProperty("form.error"));
//                LOG.debug("method saveEditForm finish > user.id :"+user.getUserId());
//                return new JSP(EDITVISIT);
//            }            
        }catch(Exception ex){
            LOG.error(ex.getMessage());
            ex.printStackTrace();//for development use only
            addSimpleError(svconf.getProperty("form.error"));
            if(user.getDepartment().getShipVisitProcessFlow().equals("T")){ //ship visit using process flow
                listCounter = sv3Manager.getListCounter(user, currentYear, departmentId);
                this.checkListCounter(listCounter,user);
                for (ShipVisit3Counter c : listCounter) {
                    counterId = c.getSv3CounterId();
                }
            }
            return new JSP(EDITVISIT);
        }
        
        LOG.debug("method saveEditForm finish > user.id :"+user.getUserId());
        return new JSP(LISTKIV);
    }
    
    public Resolution updateAction(){
        String result = "";
        User user = getContext().getCurrentUser();
        String id = getContext().getRequest().getParameter("idcrew");
        String detail = getContext().getRequest().getParameter("detail");
        String flag = getContext().getRequest().getParameter("flag");
        LOG.debug("detail :"+detail);
        
        try{
            if(flag.equals("s")){
                shipVisit3Sub = shipVisit3SubDAO.findById(Integer.parseInt(id));
                InfoAudit ia = shipVisit3Sub.getInfoAudit();
                ia.setUpdateId(user);
                ia.setUpdateDate(new Date());
                shipVisit3Sub.setInfoAudit(ia);
                shipVisit3Sub.setDetails(detail);
//                result = "Success";
                result = sv3Manager.updateSVSub(shipVisit3Sub);
            }
            if(flag.equals("se")){
                shipVisit3SubExtend = shipVisit3SubExtendDAO.findById(Integer.parseInt(id));
                InfoAudit ia = shipVisit3SubExtend.getInfoAudit();
                ia.setUpdateId(user);
                ia.setUpdateDate(new Date());
                shipVisit3SubExtend.setInfoAudit(ia);
                shipVisit3SubExtend.setDetails(detail);
//                result = "Success";
                result = sv3Manager.updateSVSubExtend(shipVisit3SubExtend);
            }
        }catch(Exception ex){
            LOG.error(ex.getMessage());
            ex.printStackTrace();//for development use only
            result = svconf.getProperty("form.error");
        }
        
        return new StreamingResolution("text/plan",result);
    }
    
    public Resolution reSubmit(){
        User user = getContext().getCurrentUser();
        LOG.debug("method reSubmit start > user.id :"+user.getUserId());
        
        staffId = user.getUserId();
        
        if(user.getDepartment().getShipVisitProcessFlow().equals("T")){
            listCounter = sv3Manager.getListCounter(user, currentYear, departmentId);
            this.checkListCounter(listCounter,user);
            for (ShipVisit3Counter sv3c : listCounter) {
                counterId = sv3c.getSv3CounterId();
            }
        } 
        
        LOG.debug("shipVisit3Id :"+shipVisit3Id);
        LOG.debug("editFlag :"+editFlag);
        LOG.debug("fromStage :"+fromStage);
        
        try{
            ShipVisit3 sv = shipVisit3DAO.findById(Integer.parseInt(shipVisit3Id));
            InfoAudit ia = sv.getInfoAudit();
            ia.setUpdateId(user);
            ia.setUpdateDate(new Date());
            
            sv.setInfoAudit(ia);
            sv.setStatus("K");
            
            ShipVisit3Counter sv3Counter = sv3Manager.getListCounter(user, currentYear, departmentId).get(0);
            InfoAudit iac = sv3Counter.getInfoAudit();
            iac.setUpdateDate(new Date());
            iac.setUpdateId(user);
            sv3Counter.setInfoAudit(iac);
            sv3Counter.setRejected(sv3Counter.getRejected()-1);
            
            String result = sv3Manager.reSubmitForm(sv,sv3Counter);
            if(result.equals("success")){
                addSimpleMessage(svconf.getProperty("form.success"));
            }else{
                addSimpleError(svconf.getProperty("form.error"));
                LOG.debug("method reSubmit finish > user.id :"+user.getUserId());
                return new JSP(EDITVISIT);
            }
        }catch(Exception ex){
            LOG.error(ex.getMessage());
            ex.printStackTrace();//for development use only
            addSimpleError(svconf.getProperty("form.error"));
            return new JSP(EDITVISIT);
        }
        
        LOG.debug("shipVisit3 :"+shipVisit3Id);
        LOG.debug("method reSubmit finish > user.id :"+user.getUserId());
        return new JSP(LISTREJECT);
    }
    
    public Resolution staffKIV(){
        User user = getContext().getCurrentUser();
        LOG.debug("method staffKIV start > user.id :"+user.getUserId());
        listKIV = sv3Manager.getListStaffKIV();
        LOG.debug("method staffKIV finish > user.id :"+user.getUserId());
        return new JSP(LISTSTAFFKIV);
    }
    
    public Resolution detailStaffVisit(){
        User user = getContext().getCurrentUser();
        LOG.debug("method detailStaff start > user.id :"+user.getUserId());
        String idVisit = getContext().getRequest().getParameter("idVisit");
        String status = getContext().getRequest().getParameter("status");
        try{            
            shipVisit3 = shipVisit3DAO.findById(Integer.parseInt(idVisit));
            LOG.debug("listHistory.size :"+shipVisit3.getListHistory().size());
            shipVisit3Id = shipVisit3.getShipVisit3Id().toString();
            staffSelected = shipVisit3.getUser();
            if(StringUtils.isNotBlank(status))
                statusDesc = status;
            
            listCounter = sv3Manager.getListCounter(shipVisit3.getUser(), currentYear, departmentId);
            this.checkListCounter(listCounter,shipVisit3.getUser());
            for (ShipVisit3Counter sv3c : listCounter) {
                counterId = sv3c.getSv3CounterId();
            }
            LOG.debug("listCounter.size :"+listCounter.size());
            fromStage = getContext().getRequest().getParameter("stage");
        }catch(Exception ex){
            LOG.error(ex.getMessage());
            ex.printStackTrace();//for development use only
            addSimpleError(svconf.getProperty("form.error"));
            listKIV = sv3Manager.getListStaffKIV();
            LOG.debug("method detailStaff finish > user.id :"+user.getUserId());
            return new JSP(LISTSTAFFKIV);
        }
        LOG.debug("method detailStaff finish > user.id :"+user.getUserId());
        return new JSP(DETAILSTAFF);
    }
    
    public Resolution submitApproval(){
        User user = getContext().getCurrentUser();
        LOG.debug("method submitApproval start > user.id :"+user.getUserId());
        LOG.debug("shipVisit3Id :"+shipVisit3Id);
        try{
            ShipVisit3 sv3 = shipVisit3DAO.findById(Integer.parseInt(shipVisit3Id));
            InfoAudit ia = sv3.getInfoAudit();
            ia.setUpdateId(user);
            ia.setUpdateDate(new Date());
            sv3.setInfoAudit(ia);
            sv3.setStatus(statusApproval);
            sv3.setRejectNote(comment);
            sv3.setVerifierId(user);
            
            ShipVisit3History sv3h = new ShipVisit3History();
            InfoAudit iah = new InfoAudit();
            iah.setCreateId(user);
            iah.setCreateDate(new Date());
            sv3h.setInfoAudit(iah);
            sv3h.setRejectNote(comment);
            sv3h.setStatus(statusApproval);
            sv3h.setVerifierId(user);
            sv3h.setShipVisit3(sv3);
            
            ShipVisit3Counter sv3Counter = sv3Manager.getListCounter(sv3.getUser(), currentYear, departmentId).get(0);
            InfoAudit iac = sv3Counter.getInfoAudit();
            iac.setUpdateDate(new Date());
            iac.setUpdateId(user);
            sv3Counter.setInfoAudit(iac);
            if(statusApproval.equals("A")){
                sv3Counter.setVisited(sv3Counter.getVisited()+1);
            }
            if(statusApproval.equals("R")){
                sv3Counter.setRejected(sv3Counter.getRejected()+1);
            }
            String result = sv3Manager.approval(sv3, sv3Counter, sv3h);
            if(result.equals("success")){
                this.sendMailStaff(sv3);
                addSimpleMessage(svconf.getProperty("form.successEmail"));
            }else{
                addSimpleError(svconf.getProperty("form.error"));
                LOG.debug("method submitApproval finish > user.id :"+user.getUserId());
                return new JSP(DETAILSTAFF);
            }
        }catch(Exception ex){
            LOG.error(ex.getMessage());
            ex.printStackTrace();//for development use only
            addSimpleError(svconf.getProperty("form.error"));
            LOG.debug("method submitApproval finish > user.id :"+user.getUserId());
            return new JSP(DETAILSTAFF);
        }
        LOG.debug("method submitApproval finish > user.id :"+user.getUserId());
//        return new JSP(LISTSTAFFKIV);
        return new RedirectResolution(ShipVisit3FlowAction.class, "staffKIV");
    }
    
    public Resolution searchStaffVisit(){
        User user = getContext().getCurrentUser();
        LOG.debug("method searchStaffVisit start > user.id :"+user.getUserId());
        LOG.debug("currentYear : "+currentYear);
        listCounter = sv3Manager.getListCounter(null, currentYear, departmentId);
        LOG.debug("listCounter :"+listCounter.size());
        LOG.debug("method searchStaffVisit finish > user.id :"+user.getUserId());
        return new JSP(SEARCHSTAFF);
    }
    
    public Resolution listStaffVisited(){
        User user = getContext().getCurrentUser();
        LOG.debug("method listStaffVisited start > user.id :"+user.getUserId());
        String idStaff = getContext().getRequest().getParameter("idStaff");
        String status   = getContext().getRequest().getParameter("status");
        LOG.debug("currentYear :"+currentYear);
        try{
            if(StringUtils.isNotBlank(status) && StringUtils.isNotBlank(idStaff)){
                staffSelected = penggunaDAO.findById(idStaff);
                if(status.equals("A")){     // A=Approved
                    listStaffVisited = sv3Manager.getListVisitApprove(staffSelected, currentYear);
                    statusDesc = "APPROVED";
                }
                if(status.equals("R")){     //  R=Rejected
                    listStaffVisited = sv3Manager.getListVisitReject(staffSelected, currentYear);
                    statusDesc = "REJECTED";
                }
            }            
        }catch(Exception ex){
            LOG.error(ex.getMessage());
            ex.printStackTrace();//for development use only
            addSimpleError(svconf.getProperty("form.error"));
            LOG.debug("method listStaffVisited finish > user.id :"+user.getUserId());
            return new RedirectResolution(ShipVisit3FlowAction.class, "searchStaffVisit");
        }
        
        LOG.debug("method listStaffVisited finish > user.id :"+user.getUserId());
        return new JSP(SEARCHVISIT);
    }
    
    private void sendMailStaff(ShipVisit3 sv3){
        if(sv3 != null){
            try{
                String status = null;
                if(sv3.getStatus().equals("R")){
                    status = "REJECTED";
                }
                if(sv3.getStatus().equals("A")){
                    status = "APPROVED";
                }
                // freemarker stuff.
                freemarker.template.Configuration cfg = new freemarker.template.Configuration();
                DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
                FileTemplateLoader templateLoader 
                        = new FileTemplateLoader(new File(conf.getProperty("email.template.path")));
                cfg.setTemplateLoader(templateLoader);
                cfg.setObjectWrapper(new DefaultObjectWrapper());
                Template template = cfg.getTemplate("html-sv-staff.ftl");
                Map<String, Object> rootMap = new HashMap<String, Object>();
                rootMap.put("vesselname", sv3.getVessel().getVesselName());
                rootMap.put("location", sv3.getLocation());
                rootMap.put("datevisit", formatter.format(sv3.getDateVisit()));
                rootMap.put("status", status);
                rootMap.put("remarks", sv3.getRejectNote());
                rootMap.put("inside", conf.getProperty("task.email.inside"));
                rootMap.put("outside", conf.getProperty("task.email.outside"));
                
                Writer out = new StringWriter();
                template.process(rootMap, out);
                String [] recipient = {sv3.getUser().getEmail()};
                LOG.debug("sendMailStaff : status :"+status);
                emailUtil.send(recipient, null, svconf.getProperty("email.staff"), out.toString(), "text/html");
                // freemarker stuff ends.
            }catch(Exception ex){
                LOG.error("error occured {}!! ", ex);
            }
        }else{
            LOG.error("Ship Visit is NULL!!");
        }
    }
    
    public Resolution newDocument(){
        staffId = getContext().getRequest().getParameter("idStaff");
        LOG.debug("method newEditDocument start > user.id :"+staffId);
        if(StringUtils.isNotBlank(staffId)){
            listVisitFiletmp = sv3Manager.getListFiletmp(staffId);
            LOG.debug("listVisitFiletmp.size :"+listVisitFiletmp.size());
        }
        LOG.debug("method newEditDocument finish > user.id :"+staffId);
        return new JSP(NEWDOC).addParameter("popup", true);
    }
    
    public Resolution saveDoc(){
        User user = getContext().getCurrentUser();
        SimpleDateFormat dt1 = new SimpleDateFormat("ddMMyyyy");
        String documentPath = conf.getProperty("document.path");
        InfoAudit ia = new InfoAudit();
        LOG.info("documentPath :"+documentPath);
        Date date = new Date();
        
        String fulldate = dt1.format(date);
        String path = documentPath + (documentPath.endsWith(File.separator) ? "" : File.separator) + FOLDERNAME + File.separator + fulldate + File.separator;
        String docPath = FOLDERNAME + File.separator + fulldate + File.separator;
        
        LOG.info("path :"+path);
        LOG.info("docPath :"+docPath);
        
        if(document != null){
            FileUtil fileUtil = new FileUtil(path);
            LOG.debug("fileUtil : pass");
            try{
                String filename = fileUtil.saveFile(document.getFileName(), document.getInputStream(), document.getSize());
                LOG.debug("filename : "+filename);
                ShipVisit3FileTemp visitFile = new ShipVisit3FileTemp();
                visitFile.setDeleted("N");
                visitFile.setDocumentPath(docPath + document.getFileName());
                visitFile.setTitle(document.getFileName());
                visitFile.setUser(user);
                visitFile.setContentType(document.getContentType());
                ia.setCreateDate(new Date());
                ia.setCreateId(user);
                visitFile.setInfoAudit(ia);
                LOG.debug("file title :"+visitFile.getTitle());
                String result = sv3Manager.saveorUpdateFileTemp(visitFile);
                if(result.equals("success"))
                    addSimpleMessage(svconf.getProperty("upload.success"));
                else
                    addSimpleError(svconf.getProperty("upload.error"));
            }catch (Exception ex) {
                ex.printStackTrace();
//                LOG.error("(shipVisitFlowAction) error while saving file {}", ex.getCause().getMessage());
                addSimpleError(svconf.getProperty("upload.notvalid"));
            }
        }
        return new RedirectResolution(ShipVisit3FlowAction.class, "newDocument")
                .addParameter("idStaff", user.getUserId());
    }
    
    public Resolution deleteFileTemp(){
        staffId = getContext().getRequest().getParameter("idStaff");
        String fileId = getContext().getRequest().getParameter("idFile");
        
        if(StringUtils.isNotBlank(fileId)){
            ShipVisit3FileTemp shipVisit3FileTemp = shipVisit3FileTempDAO.findById(Integer.parseInt(fileId));
            shipVisit3FileTemp.setDeleted("Y");
            
            InfoAudit ia = shipVisit3FileTemp.getInfoAudit();
            ia.setUpdateDate(new Date());
            ia.setUpdateId(getContext().getCurrentUser());
            shipVisit3FileTemp.setInfoAudit(ia);
            String result = sv3Manager.saveorUpdateFileTemp(shipVisit3FileTemp);
            if(result.equals("success"))
                addSimpleMessage(svconf.getProperty("delete.success"));
            else
                addSimpleError(svconf.getProperty("delete.error"));
        }else{
            addSimpleError(svconf.getProperty("delete.idnull"));
        }
        
        return newDocument();
    }
    
    public Resolution newDocumentSub(){
        staffId = getContext().getRequest().getParameter("idStaff");
        refSubId = getContext().getRequest().getParameter("idSubRef");
        LOG.debug("method newEditDocumentSub start > user.id :"+staffId);
        if(StringUtils.isNotBlank(staffId) && StringUtils.isNotBlank(refSubId)){
            listVisitFileSubtmp = sv3Manager.getListFileSubtmp(staffId,refSubId);
            LOG.debug("listVisitFileSubtmp.size :"+listVisitFileSubtmp.size());
        }
        LOG.debug("method newEditDocumentSub finish > user.id :"+staffId);
        return new JSP(NEWDOCSUB).addParameter("popup", true);
    }
    
    public Resolution saveDocSub(){
        User user = getContext().getCurrentUser();
        SimpleDateFormat dt1 = new SimpleDateFormat("ddMMyyyy");
        String documentPath = conf.getProperty("document.path");
        InfoAudit ia = new InfoAudit();
        LOG.info("documentPath :"+documentPath);
        Date date = new Date();
        
        String fulldate = dt1.format(date);
        String path = documentPath + (documentPath.endsWith(File.separator) ? "" : File.separator) + FOLDERNAME + File.separator + FOLDERNAMESUB + File.separator + fulldate + File.separator;
        String docPath = FOLDERNAME + File.separator + FOLDERNAMESUB + File.separator + fulldate + File.separator;
        
        LOG.info("path :"+path);
        LOG.info("docPath :"+docPath);
        LOG.debug("refSubId :"+refSubId);
        
        if(document != null){
            FileUtil fileUtil = new FileUtil(path);
            LOG.debug("fileUtil : pass");
            try{
                String filename = fileUtil.saveFile(document.getFileName(), document.getInputStream(), document.getSize());
                LOG.debug("filename : "+filename);
                ShipVisit3SubExtendFileTemp visitFile = new ShipVisit3SubExtendFileTemp();
                RefShipVisitSub rsvs = refShipVisitSubDAO.findById(Integer.parseInt(refSubId));
                visitFile.setShipVisitSub(rsvs);
                visitFile.setDeleted("N");
                visitFile.setDocumentPath(docPath + document.getFileName());
                visitFile.setTitle(document.getFileName());
                visitFile.setUser(user);
                visitFile.setContentType(document.getContentType());
                ia.setCreateDate(new Date());
                ia.setCreateId(user);
                visitFile.setInfoAudit(ia);
                LOG.debug("file title :"+visitFile.getTitle());
                String result = sv3Manager.saveorUpdateFileSubTemp(visitFile);
                if(result.equals("success"))
                    addSimpleMessage(svconf.getProperty("upload.success"));
                else
                    addSimpleError(svconf.getProperty("upload.error"));
            }catch (Exception ex) {
                ex.printStackTrace();
//                LOG.error("(shipVisitFlowAction) error while saving file {}", ex.getCause().getMessage());
                addSimpleError(svconf.getProperty("upload.notvalid"));
            }
        }
        return new RedirectResolution(ShipVisit3FlowAction.class, "newDocumentSub")
                .addParameter("idStaff", user.getUserId()).addParameter("idSubRef", refSubId);
    }
    
    public Resolution deleteFileSubTemp(){
        staffId = getContext().getRequest().getParameter("idStaff");
        refSubId = getContext().getRequest().getParameter("idSubRef");
        String fileId = getContext().getRequest().getParameter("idFile");
        
        if(StringUtils.isNotBlank(fileId)){
            ShipVisit3SubExtendFileTemp shipVisit3FileTemp = subFileTempDAO.findById(Integer.parseInt(fileId));
            shipVisit3FileTemp.setDeleted("Y");
            
            InfoAudit ia = shipVisit3FileTemp.getInfoAudit();
            ia.setUpdateDate(new Date());
            ia.setUpdateId(getContext().getCurrentUser());
            shipVisit3FileTemp.setInfoAudit(ia);
            String result = sv3Manager.saveorUpdateFileSubTemp(shipVisit3FileTemp);
            if(result.equals("success"))
                addSimpleMessage(svconf.getProperty("delete.success"));
            else
                addSimpleError(svconf.getProperty("delete.error"));
        }else{
            addSimpleError(svconf.getProperty("delete.idnull"));
        }
        
        return newDocumentSub();
    }
    
    public Resolution viewEditDocument(){
        User user = getContext().getCurrentUser();
        shipVisit3Id = getContext().getRequest().getParameter("idVisit");
        editFlag = getContext().getRequest().getParameter("flag");
        LOG.debug("method viewEditDocument start > user.id :"+user.getUserId());
        if(StringUtils.isNotBlank(shipVisit3Id)){
            shipVisit3 = shipVisit3DAO.findById(Integer.parseInt(shipVisit3Id));
            LOG.debug("shipVisit3.getListFile.size :"+shipVisit3.getListFile().size());
        }
        LOG.debug("method viewEditDocument finish > user.id :"+staffId);
        return new JSP(VIEWEDITDOC).addParameter("popup", true);
    }
    
    public Resolution updateDoc(){
        User user = getContext().getCurrentUser();
        SimpleDateFormat dt1 = new SimpleDateFormat("ddMMyyyy");
        String documentPath = conf.getProperty("document.path");
        InfoAudit ia = new InfoAudit();
        LOG.info("documentPath :"+documentPath);
        Date date = new Date();
        
        String fulldate = dt1.format(date);
        String path = documentPath + (documentPath.endsWith(File.separator) ? "" : File.separator) + FOLDERNAME + File.separator + fulldate + File.separator;
        String docPath = FOLDERNAME + File.separator + fulldate + File.separator;
        
        LOG.info("path :"+path);
        LOG.info("docPath :"+docPath);        
        LOG.debug("shipVisitId :"+shipVisit3Id);
        LOG.debug("editFlag :"+editFlag);
        
        if(document != null){
            FileUtil fileUtil = new FileUtil(path);
            LOG.debug("fileUtil : pass");
            try{
                String filename = fileUtil.saveFile(document.getFileName(), document.getInputStream(), document.getSize());
                LOG.debug("filename : "+filename);
                ShipVisit3 sv3 = shipVisit3DAO.findById(Integer.parseInt(shipVisit3Id));
                ShipVisit3File visitFile = new ShipVisit3File();
                visitFile.setDeleted("N");
                visitFile.setDocumentPath(docPath + document.getFileName());
                visitFile.setTitle(document.getFileName());
                visitFile.setContentType(document.getContentType());
                visitFile.setShipVisit3(sv3);
                ia.setCreateDate(new Date());
                ia.setCreateId(user);
                visitFile.setInfoAudit(ia);
                LOG.debug("file title :"+visitFile.getTitle());
                String result = sv3Manager.saveorUpdateFile(visitFile);
                if(result.equals("success"))
                    addSimpleMessage(svconf.getProperty("upload.success"));
                else
                    addSimpleError(svconf.getProperty("upload.error"));
            }catch (Exception ex) {
                ex.printStackTrace();
//                LOG.error("(shipVisitFlowAction) error while saving file {}", ex.getCause().getMessage());
                addSimpleError(svconf.getProperty("upload.notvalid"));
            }
        }
        return new RedirectResolution(ShipVisit3FlowAction.class, "viewEditDocument")
                .addParameter("idVisit", shipVisit3Id).addParameter("flag", editFlag);
    }
    
    public Resolution deleteFile(){
        shipVisit3Id = getContext().getRequest().getParameter("idVisit");
        editFlag = getContext().getRequest().getParameter("flag");
        String fileId = getContext().getRequest().getParameter("idFile");
        
        if(StringUtils.isNotBlank(fileId)){
            ShipVisit3File shipVisit3File = shipVisit3FileDAO.findById(Integer.parseInt(fileId));
            shipVisit3File.setDeleted("Y");
            
            InfoAudit ia = shipVisit3File.getInfoAudit();
            ia.setUpdateDate(new Date());
            ia.setUpdateId(getContext().getCurrentUser());
            shipVisit3File.setInfoAudit(ia);
            String result = sv3Manager.saveorUpdateFile(shipVisit3File);
            if(result.equals("success"))
                addSimpleMessage(svconf.getProperty("delete.success"));
            else
                addSimpleError(svconf.getProperty("delete.error"));
        }else{
            addSimpleError(svconf.getProperty("delete.idnull"));
        }
        
        return viewEditDocument();
    }
    
    public Resolution viewEditDocumentSub(){
        User user = getContext().getCurrentUser();
        subExtendId = getContext().getRequest().getParameter("idVisitExtend");
        editFlag = getContext().getRequest().getParameter("flag");
        LOG.debug("method viewEditDocumentSub start > user.id :"+user.getUserId());
        if(StringUtils.isNotBlank(subExtendId)){
            shipVisit3SubExtend = shipVisit3SubExtendDAO.findById(Integer.parseInt(subExtendId));
            LOG.debug("shipVisit3SubExtend.getListFile.size :"+shipVisit3SubExtend.getListFile().size());
        }
        LOG.debug("method viewEditDocumentSub finish > user.id :"+user.getUserId());
        return new JSP(VIEWEDITDOCSUB).addParameter("popup", true);
    }
    
    public Resolution updateDocSub(){
        User user = getContext().getCurrentUser();
        SimpleDateFormat dt1 = new SimpleDateFormat("ddMMyyyy");
        String documentPath = conf.getProperty("document.path");
        InfoAudit ia = new InfoAudit();
        LOG.info("documentPath :"+documentPath);
        Date date = new Date();
        
        String fulldate = dt1.format(date);
        String path = documentPath + (documentPath.endsWith(File.separator) ? "" : File.separator) + FOLDERNAME + File.separator + FOLDERNAMESUB + File.separator + fulldate + File.separator;
        String docPath = FOLDERNAME + File.separator + FOLDERNAMESUB + File.separator + fulldate + File.separator;
        
        LOG.info("path :"+path);
        LOG.info("docPath :"+docPath);
        LOG.debug("subExtendId :"+subExtendId);
        LOG.debug("editFlag :"+editFlag);
        
        if(document != null){
            FileUtil fileUtil = new FileUtil(path);
            LOG.debug("fileUtil : pass");
            try{
                String filename = fileUtil.saveFile(document.getFileName(), document.getInputStream(), document.getSize());
                LOG.debug("filename : "+filename);
                ShipVisit3SubExtend sv3se = shipVisit3SubExtendDAO.findById(Integer.parseInt(subExtendId));
                ShipVisit3SubExtendFile visitFile = new ShipVisit3SubExtendFile();
                visitFile.setDeleted("N");
                visitFile.setDocumentPath(docPath + document.getFileName());
                visitFile.setTitle(document.getFileName());
                visitFile.setContentType(document.getContentType());
                ia.setCreateDate(new Date());
                ia.setCreateId(user);
                visitFile.setInfoAudit(ia);
                visitFile.setShipVisit3SubExtend(sv3se);
                LOG.debug("file title :"+visitFile.getTitle());
                String result = sv3Manager.saveorUpdateFileSub(visitFile);
                if(result.equals("success"))
                    addSimpleMessage(svconf.getProperty("upload.success"));
                else
                    addSimpleError(svconf.getProperty("upload.error"));
            }catch (Exception ex) {
                ex.printStackTrace();
//                LOG.error("(shipVisitFlowAction) error while saving file {}", ex.getCause().getMessage());
                addSimpleError(svconf.getProperty("upload.notvalid"));
            }
        }
        return new RedirectResolution(ShipVisit3FlowAction.class, "viewEditDocumentSub")
                .addParameter("idVisitExtend", subExtendId).addParameter("flag", editFlag);
    }
    
    public Resolution deleteFileSub(){
        subExtendId = getContext().getRequest().getParameter("idVisitExtend");
        editFlag = getContext().getRequest().getParameter("flag");
        String fileId = getContext().getRequest().getParameter("idFile");
        
        if(StringUtils.isNotBlank(fileId)){
            ShipVisit3SubExtendFile shipVisit3File = shipVisit3SubExtendFileDAO.findById(Integer.parseInt(fileId));
            shipVisit3File.setDeleted("Y");
            
            InfoAudit ia = shipVisit3File.getInfoAudit();
            ia.setUpdateDate(new Date());
            ia.setUpdateId(getContext().getCurrentUser());
            shipVisit3File.setInfoAudit(ia);
            String result = sv3Manager.saveorUpdateFileSub(shipVisit3File);
            if(result.equals("success"))
                addSimpleMessage(svconf.getProperty("delete.success"));
            else
                addSimpleError(svconf.getProperty("delete.error"));
        }else{
            addSimpleError(svconf.getProperty("delete.idnull"));
        }
        
        return viewEditDocumentSub();
    }
    
    public List<RefShipVisitMatch> getListShipVisitMatch() {
        return listShipVisitMatch;
    }

    public void setListShipVisitMatch(List<RefShipVisitMatch> listShipVisitMatch) {
        this.listShipVisitMatch = listShipVisitMatch;
    }

    public ShipVisit3 getShipVisit3() {
        return shipVisit3;
    }

    public void setShipVisit3(ShipVisit3 shipVisit3) {
        this.shipVisit3 = shipVisit3;
    }

    public List<ShipVisit3> getListShipVisit3() {
        return listShipVisit3;
    }

    public void setListShipVisit3(List<ShipVisit3> listShipVisit3) {
        this.listShipVisit3 = listShipVisit3;
    }

    public FileBean getDocument() {
        return document;
    }

    public void setDocument(FileBean document) {
        this.document = document;
    }

    public String getCurrentYear() {
        return currentYear;
    }

    public void setCurrentYear(String currentYear) {
        this.currentYear = currentYear;
    }

    public String getStaffId() {
        return staffId;
    }

    public void setStaffId(String staffId) {
        this.staffId = staffId;
    }

    public List<String> getListDetails() {
        return listDetails;
    }

    public void setListDetails(List<String> listDetails) {
        this.listDetails = listDetails;
    }

    public List<String> getListSubDetails() {
        return listSubDetails;
    }

    public void setListSubDetails(List<String> listSubDetails) {
        this.listSubDetails = listSubDetails;
    }

    public List<ShipVisit3> getListKIV() {
        return listKIV;
    }

    public void setListKIV(List<ShipVisit3> listKIV) {
        this.listKIV = listKIV;
    }

    public List<ShipVisit3Counter> getListCounter() {
        return listCounter;
    }

    public void setListCounter(List<ShipVisit3Counter> listCounter) {
        this.listCounter = listCounter;
    }

    public Integer getDefaultCount() {
        return defaultCount;
    }

    public void setDefaultCount(Integer defaultCount) {
        this.defaultCount = defaultCount;
    }

    public String getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(String departmentId) {
        this.departmentId = departmentId;
    }

    public ShipVisit3Counter getShipVisit3Counter() {
        return shipVisit3Counter;
    }

    public void setShipVisit3Counter(ShipVisit3Counter shipVisit3Counter) {
        this.shipVisit3Counter = shipVisit3Counter;
    }

    public Integer getCounterId() {
        return counterId;
    }

    public void setCounterId(Integer counterId) {
        this.counterId = counterId;
    }

    public Integer getSizeMatch() {
        return sizeMatch;
    }

    public void setSizeMatch(Integer sizeMatch) {
        this.sizeMatch = sizeMatch;
    }

    public Integer getSizeSubMatch() {
        return sizeSubMatch;
    }

    public void setSizeSubMatch(Integer sizeSubMatch) {
        this.sizeSubMatch = sizeSubMatch;
    }

    public String getShipVisit3Id() {
        return shipVisit3Id;
    }

    public void setShipVisit3Id(String shipVisit3Id) {
        this.shipVisit3Id = shipVisit3Id;
    }

    public List<ShipVisit3Sub> getListSub() {
        return listSub;
    }

    public void setListSub(List<ShipVisit3Sub> listSub) {
        this.listSub = listSub;
    }

    public List<ShipVisit3SubExtend> getListSubExtend() {
        return listSubExtend;
    }

    public void setListSubExtend(List<ShipVisit3SubExtend> listSubExtend) {
        this.listSubExtend = listSubExtend;
    }

    public ShipVisit3Sub getShipVisit3Sub() {
        return shipVisit3Sub;
    }

    public void setShipVisit3Sub(ShipVisit3Sub shipVisit3Sub) {
        this.shipVisit3Sub = shipVisit3Sub;
    }

    public ShipVisit3SubExtend getShipVisit3SubExtend() {
        return shipVisit3SubExtend;
    }

    public void setShipVisit3SubExtend(ShipVisit3SubExtend shipVisit3SubExtend) {
        this.shipVisit3SubExtend = shipVisit3SubExtend;
    }

    public List<ShipVisit3> getListApprove() {
        return listApprove;
    }

    public void setListApprove(List<ShipVisit3> listApprove) {
        this.listApprove = listApprove;
    }

    public List<ShipVisit3> getListReject() {
        return listReject;
    }

    public void setListReject(List<ShipVisit3> listReject) {
        this.listReject = listReject;
    }

    public String getEditFlag() {
        return editFlag;
    }

    public void setEditFlag(String editFlag) {
        this.editFlag = editFlag;
    }

    public String getFromStage() {
        return fromStage;
    }

    public void setFromStage(String fromStage) {
        this.fromStage = fromStage;
    }

    public String getStatusDesc() {
        return statusDesc;
    }

    public void setStatusDesc(String statusDesc) {
        this.statusDesc = statusDesc;
    }

    public List<ShipVisit3> getListStaffVisited() {
        return listStaffVisited;
    }

    public void setListStaffVisited(List<ShipVisit3> listStaffVisited) {
        this.listStaffVisited = listStaffVisited;
    }

    public User getStaffSelected() {
        return staffSelected;
    }

    public void setStaffSelected(User staffSelected) {
        this.staffSelected = staffSelected;
    }

    public String getStatusApproval() {
        return statusApproval;
    }

    public void setStatusApproval(String statusApproval) {
        this.statusApproval = statusApproval;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public List<ShipVisit3FileTemp> getListVisitFiletmp() {
        return listVisitFiletmp;
    }

    public void setListVisitFiletmp(List<ShipVisit3FileTemp> listVisitFiletmp) {
        this.listVisitFiletmp = listVisitFiletmp;
    }

    public String getForXlsx() {
        return forXlsx;
    }

    public void setForXlsx(String forXlsx) {
        this.forXlsx = forXlsx;
    }

    public String getForDocx() {
        return forDocx;
    }

    public void setForDocx(String forDocx) {
        this.forDocx = forDocx;
    }

    public String getForPptx() {
        return forPptx;
    }

    public void setForPptx(String forPptx) {
        this.forPptx = forPptx;
    }

    public String getForXls() {
        return forXls;
    }

    public void setForXls(String forXls) {
        this.forXls = forXls;
    }

    public String getForDoc() {
        return forDoc;
    }

    public void setForDoc(String forDoc) {
        this.forDoc = forDoc;
    }

    public String getForPpt() {
        return forPpt;
    }

    public void setForPpt(String forPpt) {
        this.forPpt = forPpt;
    }

    public String getForPdf() {
        return forPdf;
    }

    public void setForPdf(String forPdf) {
        this.forPdf = forPdf;
    }

    public String getForVideoMov() {
        return forVideoMov;
    }

    public void setForVideoMov(String forVideoMov) {
        this.forVideoMov = forVideoMov;
    }

    public String getForVideoMkv() {
        return forVideoMkv;
    }

    public void setForVideoMkv(String forVideoMkv) {
        this.forVideoMkv = forVideoMkv;
    }

    public String getForVideoMp4() {
        return forVideoMp4;
    }

    public void setForVideoMp4(String forVideoMp4) {
        this.forVideoMp4 = forVideoMp4;
    }

    public String getForVideoAvi() {
        return forVideoAvi;
    }

    public void setForVideoAvi(String forVideoAvi) {
        this.forVideoAvi = forVideoAvi;
    }

    public String getForVideoMpeg() {
        return forVideoMpeg;
    }

    public void setForVideoMpeg(String forVideoMpeg) {
        this.forVideoMpeg = forVideoMpeg;
    }

    public String getForVideoWmv() {
        return forVideoWmv;
    }

    public void setForVideoWmv(String forVideoWmv) {
        this.forVideoWmv = forVideoWmv;
    }

    public String getForVideoAsf() {
        return forVideoAsf;
    }

    public void setForVideoAsf(String forVideoAsf) {
        this.forVideoAsf = forVideoAsf;
    }

    public String getForVideoFlv() {
        return forVideoFlv;
    }

    public void setForVideoFlv(String forVideoFlv) {
        this.forVideoFlv = forVideoFlv;
    }

    public String getForVideoRmvb() {
        return forVideoRmvb;
    }

    public void setForVideoRmvb(String forVideoRmvb) {
        this.forVideoRmvb = forVideoRmvb;
    }

    public String getForVideo3gp() {
        return forVideo3gp;
    }

    public void setForVideo3gp(String forVideo3gp) {
        this.forVideo3gp = forVideo3gp;
    }

    public String getRefSubId() {
        return refSubId;
    }

    public void setRefSubId(String refSubId) {
        this.refSubId = refSubId;
    }

    public List<ShipVisit3SubExtendFileTemp> getListVisitFileSubtmp() {
        return listVisitFileSubtmp;
    }

    public void setListVisitFileSubtmp(List<ShipVisit3SubExtendFileTemp> listVisitFileSubtmp) {
        this.listVisitFileSubtmp = listVisitFileSubtmp;
    }

    public String getSubExtendId() {
        return subExtendId;
    }

    public void setSubExtendId(String subExtendId) {
        this.subExtendId = subExtendId;
    }
    
}
